package ca.jsbr.tinyUI.container 
{
	import ca.jsbr.as2js_lib.display.canvas.View;
	import ca.jsbr.display.sizers.HSizers;
	import flash.display.DisplayObject;
	/**
	 * ...
	 * @author jsbr [me@jsbr.ca]
	 */
	public class HBox extends Box
	{
		private var _background:Boolean;
		private var _childs:Vector.<View>;
		
		protected var _margin:Array = [0, 0, 0, 0];
		protected var _cellMargin:Array = [0, 0, 0, 0];
		
		public function HBox(background:Boolean=false) 
		{
			_background = background;
			_childs = new Vector.<DisplayObject>();
			width = 5;
			height = 5;
			draw();
		}
		
		override protected function draw():void 
		{
			if(_background)
				super.draw();
		}
		
		override public function addChild(view:View):DisplayObject 
		{
			addChildBox(view);
			return super.addChild(view);
		}
		
		override public function addChildAt(child:DisplayObject, index:int):flash.display.DisplayObject 
		{
			addChildBox(child);
			return super.addChildAt(child, index);
		}
		
		private function addChildBox(child:DisplayObject):void 
		{
			//if (child.hasOwnProperty("redraw") && style.autoReDraw)
			//	TinyObject(child).redraw();
			if (_childs.length)	{
				child.x = _childs[_childs.length - 1].x + _childs[_childs.length - 1].width+_cellMargin[3]+_cellMargin[1];
			}
			else {
				child.x = _margin[3];
			}
			child.y = _margin[0];
			_childs.push(child);
			width = _childs[_childs.length - 1].x + _childs[_childs.length - 1].width + _margin[1]-1;
			if (height < child.height)
				height = child.height + _margin[2] + child.y-1;
		}
		
		override public function redraw(child:Boolean=false):void 
		{
			super.redraw(child);
			draw();
			var childs:Vector.<DisplayObject> = _childs.concat();
			_childs = new Vector.<DisplayObject>();
			for each(var d:DisplayObject in childs)
			{
				addChildBox(d);
			}
		}
		
		public function setMargin(...arg:Array):void 
		{
			if (arg.length == 4)
				_margin = arg;
			else if (arg.length == 2)
				_margin = [arg[0], arg[1], arg[0], arg[1]];
			else if (arg.length == 1)
				_margin = [arg[0], arg[0], arg[0], arg[0]];
			if(style.autoReDraw)
				redraw();
		}
		
		public function setCellMargin(...arg:Array):void 
		{
			if (arg.length == 4)
				_cellMargin = arg;
			else if (arg.length == 2)
				_cellMargin = [arg[0], arg[1], arg[0], arg[1]];
			else if (arg.length == 1)
				_cellMargin = [arg[0], arg[0], arg[0], arg[0]];
			if(style.autoReDraw)
				redraw();
		}
		
	}

}